package com.fyi.controller;

import com.fyi.entity.Article;
import com.fyi.entity.Message;
import com.fyi.entity.User;
import com.fyi.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 前台路径
     * @return
     */
    @RequestMapping("userMassageView")
    public String userMassageView(){
        return "userMessage";
    }

    /**
     * 后台路径
     * @return
     */
    @RequestMapping("messageBackView")
    public String messageBackView(){
        return "backView/showMessage";
    }

    /**
     * 查看所有留言
     * @return
     */
    @RequestMapping("selectAllMessage")
    @ResponseBody
    public Map selectAllMessage(){

        List<Message> selectAllMessage = messageService.selectAllMessage();
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectAllMessage);
        return result;
    }
    /**
     * 根据Id查询
     */
    @RequestMapping("/selectMessageById/{messageId}")
    @ResponseBody
    public Map selectMessageById(@PathVariable Integer messageId){
        Message selectMesById = messageService.selectMesById(messageId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectMesById);
        return result;
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteMessage/{messageId}")
    @ResponseBody
    public Map deleteArtice(@PathVariable Integer messageId){
        Integer deleteMessage = messageService.deleteMessage(messageId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", deleteMessage);
        return result;
    }

    /**
     * 添加留言
     * @param message
     * @param session
     * @return
     */
    @RequestMapping("/addMessage")
    @ResponseBody
    public Map addMessage(@ModelAttribute Message message, HttpSession session){

        System.out.println(message);
        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        String time=sdf.format(d);
        System.out.println("--"+time);
        message.setCreateMessageTime(time);

        User user =(User)session.getAttribute("User");
        message.setUserId(user.getUserId());
        System.out.println(message);
        Integer addMessage = messageService.addMessage(message);

        Map addresult = new HashMap();
        addresult.put("code",200);
        addresult.put("msg","添加成功");
        addresult.put("data",addMessage);
        return addresult;
    }

}
